﻿using AutoMapper;
using B.S.Traceability.Domain;
using B.S.Traceability.Error;
using B.S.Traceability.Infrastructure.Interface;
using B.S.Traceability.Writer.API.Application.Commends;
using MediatR;
using Yitter.IdGenerator;

namespace B.S.Traceability.Writer.API.Application.CommendHandlers
{


    public class UpdataProductsCommendHandlers : IRequestHandler<UpdataProductsCommends, APIResult<Products>>
    {

        private readonly IBaseRepository<Products> productsRepository;
        private readonly IMapper mapper;

        public UpdataProductsCommendHandlers(IBaseRepository<Products> productsRepository, IMapper mapper)
        {
            this.productsRepository = productsRepository;
            this.mapper = mapper;
        }


        /// <summary>
        /// 修改生产信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<Products>> Handle(UpdataProductsCommends request, CancellationToken cancellationToken)
        {
            APIResult<Products> result = new APIResult<Products>();
            result.Code = APIEnum.修改成功;
            result.Msg=APIEnum.修改成功.ToString();
            var products = mapper.Map<Products>(request);
            Random rand = new Random();
            products.UpdateName = request.CreateName;
            products.UpdateTime = DateTime.Now;
       
      
          await productsRepository.Update(products);

            return await Task.FromResult(result);
        }
    }
}
